#!/usr/bin/env python3

import codecs
import json
import http.client,urllib.parse,urllib.error
import time
import random

class do_get_aminer_data(object):
    def __init__(self,name,university_name):
        self.name = name
        self.university_name = university_name
    def do_connect(self):
        params = urllib.parse.urlencode({
            'name': self.name,
            #'aff': self.university_name
            'org':self.university_name
        })
        #https://innovaapi.aminer.cn/tools/v1/predict/gender?
        try:

            #conn = http.client.HTTPSConnection('api.aminer.org')
            #conn.request("GET", "/api/kit/gender/api/ch?%s"%params)
            conn = http.client.HTTPSConnection('https://innovaapi.aminer.cn')
            conn.request("GET", "/tools/v1/predict/gender?%s"%params)
            response = conn.getresponse()
            if response.status == 200:

                data = str(response.read(),encoding = "utf-8")
                conn.close()
                data_json = json.loads(data)
                time.sleep(random.randrange(0,8))
                return data_json,response.status
            else:
                print("服务器响应的状态码为:",response.status)
                time.sleep(10)
                conn.close()
                return {},response.status

        except:
            print("服务器无法响应")
            time.sleep(10)
            return {},40303
